package models

import (
	"fmt"
	"strconv"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// phexamstuid phexamstuname phexamstuage phexamstusex phexamstusdept phexamheight phexamdate phexamlength phexamweight
// 体检数据表
type PhExam struct {
	gorm.Model
	PhExamStuID    string `json:"phexamstuid" gorm:"type:varchar(20); not null; unique"`
	PhExamStuName  string `json:"phexamstuname" gorm:"type:varchar(20); not null"`
	PhExamStuAge   int    `json:"phexamstuage" gorm:"type:integer; not null"`
	PhExamStuSex   string `json:"phexamstusex" gorm:"type:varchar(20); not null"`
	PhExamStuSdept string `json:"phexamstusdept" gorm:"type:varchar(20); not null"`
	PhExamHeight   int    `json:"phexamheight" gorm:"type:integer; not null"`
	PhExamDate     string `json:"phexamdate" gorm:"type:varchar(100); not null"`
	PhExamLength   int    `json:"phexamlength" gorm:"type:integer; not null"`
	PhExamWeight   int    `json:"phexamweight" gorm:"type:integer; not null"`
	IsDel          bool   `json:"isdel" form:"isdel" gorm:"type:boolean default false;"`
}

func getJson2(c *gin.Context) (PhExam, error) {
	json := make(map[string]string, 1024)
	c.ShouldBind(&json)
	fmt.Println(json)
	phExamStuID := json["phexamstuid"]
	phExamStuName := json["phexamstuname"]
	phExamStuAge := json["phexamstuage"]
	phExamStuSex := json["phexamstusex"]
	phExamStuSdept := json["phexamstusdept"]
	phExamHeight := json["phexamheight"]
	phExamDate := json["phexamdate"]
	phExamLength := json["phexamlength"]
	phExamWeight := json["phexamweight"]

	height, err := strconv.Atoi(phExamHeight)
	if err != nil {
		return PhExam{}, err
	}

	weight, err := strconv.Atoi(phExamWeight)
	if err != nil {
		return PhExam{}, err
	}

	age, err := strconv.Atoi(phExamStuAge)
	if err != nil {
		return PhExam{}, err
	}

	length, err := strconv.Atoi(phExamLength)

	phexam := PhExam{
		PhExamStuID:    phExamStuID,
		PhExamStuName:  phExamStuName,
		PhExamStuAge:   age,
		PhExamStuSex:   phExamStuSex,
		PhExamStuSdept: phExamStuSdept,
		PhExamHeight:   height,
		PhExamWeight:   weight,
		PhExamDate:     phExamDate,
		PhExamLength:   length,
	}

	return phexam, err
}

// PhExamStuID
func AddPhExam(c *gin.Context) (PhExam, int64, error) {
	phExam, err := getJson2(c)
	var total int64
	db.Table("ph_exams").Where("ph_exam_stu_id = ?", phExam.PhExamStuID).Count(&total)
	if total == 0 {
		err := db.Table("ph_exams").Create(&phExam).Error
		return phExam, total, err
	}
	return phExam, total, err
}

func UpdatePhExam(c *gin.Context) (PhExam, error) {
	phExam, err := getJson2(c)
	if err != nil {
		return PhExam{}, err
	}
	err = db.Table("ph_exams").Where("ph_exam_stu_id = ?", phExam.PhExamStuID).Updates(&phExam).Error
	return phExam, err
}

func GetAllPhExam(PhExamParam map[string]interface{}) ([]PhExam, int64, error) {
	var phExamData []PhExam
	pagenum := PhExamParam["pagenum"].(int)
	pagesize := PhExamParam["pagesize"].(int)
	query := PhExamParam["query"].(string)

	if query == "" {
		var total int64

		err := db.Table("ph_exams").Where("is_del = false").Count(&total).Error
		if err != nil {
			return phExamData, 0, err
		}

		err = db.Table("ph_exams").Where("is_del = false").Offset((pagenum - 1) * pagesize).Limit(pagesize).Find(&phExamData).Error
		return phExamData, total, err
	}
	stuID := query

	var phExam PhExam
	err := db.Table("ph_exams").Where("ph_exam_stu_id = ?", stuID).First(&phExam).Error
	phExamData = append(phExamData, phExam)
	return phExamData, 1, err
}

func DelPhExam(c *gin.Context) error {
	id := c.Params.ByName("id")
	err := db.Table("ph_exams").Where("ph_exam_stu_id = ?", id).Update("is_del", true).Error
	return err
}
